package com.slicktechnologies.client.views.customer;

import java.util.ArrayList;
import java.util.Vector;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.FormFieldBuilder;
import com.simplesoftwares.client.library.appstructure.SuperModel;
import com.simplesoftwares.client.library.appstructure.search.Filter;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.client.library.composite.DateComparator;
import com.simplesoftwares.client.library.composite.PersonInfoComposite;
import com.simplesoftwares.client.library.libservice.GenricService;
import com.simplesoftwares.client.library.libservice.GenricServiceAsync;
import com.simplesoftwares.client.library.mywidgets.ObjectListBox;
import com.slicktechnologies.client.config.CategoryTypeFactory;
import com.slicktechnologies.client.utility.AppUtility;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.client.views.customer.CustomerPresenter.CustomerPresenterSearch;
import com.slicktechnologies.shared.common.businessprocesslayer.Customer;
import com.slicktechnologies.shared.common.businessunitlayer.Branch;
import com.slicktechnologies.shared.common.helperlayer.Config;
import com.slicktechnologies.shared.common.helperlayer.ConfigCategory;
import com.slicktechnologies.shared.common.helperlayer.Type;
import com.slicktechnologies.shared.common.personlayer.Employee;
import com.slicktechnologies.shared.common.relationalLayer.CustomerRelation;

public class CustomerPresenterSearchProxy extends CustomerPresenterSearch {
	public ObjectListBox<Branch> olbBranch;
	public ObjectListBox<Employee> olbSalesPerson;
	public ListBox lbStatus;
	public DateComparator dateComparator;
	public PersonInfoComposite personInfo;
	private SuggestBox sbclientname;
	public Object getVarRef(String varName)
	{
		if(varName.equals("olbBranch"))
			return this.olbBranch;
		if(varName.equals("olbSalesPerson"))
			return this.olbSalesPerson;
		if(varName.equals("lbStatus"))
			return this.lbStatus;
		if(varName.equals("dateComparator"))
			return this.dateComparator;
		
		/*if(varName.equals("personInfo"))
			return this.personInfo;*/
		
		return null ;
	}
	public CustomerPresenterSearchProxy()
	{
		super();
		createGui();
	}
	public void initWidget()
	{
		olbBranch= new ObjectListBox<Branch>();
		AppUtility.makeBranchListBoxLive(olbBranch);
		olbSalesPerson= new ObjectListBox<Employee>();
		AppUtility.makeSalesPersonListBoxLive(olbSalesPerson);
		lbStatus= new ListBox();
		AppUtility.setStatusListBox(lbStatus,Customer.getStatusList());
		dateComparator= new DateComparator("businessProcess.creationDate",new Customer());
		MyQuerry querry=new MyQuerry();
		querry.setQuerryObject(new Customer());
		personInfo=new PersonInfoComposite(querry,false);
		
		sbclientname=new SuggestBox();
		initializeClientNameOracle();
		

		//For giving type category functionality
		
        
		personInfo.getCustomerId().getHeaderLabel().setText("Customer ID");
		personInfo.getCustomerName().getHeaderLabel().setText("Customer Name");
		personInfo.getCustomerCell().getHeaderLabel().setText("Customer Cell");
	}
	
	private void initializeClientNameOracle()
	{

		 final MultiWordSuggestOracle orcl = (MultiWordSuggestOracle) this.sbclientname.getSuggestOracle();
		 GenricServiceAsync service=GWT.create(GenricService.class);
		 service.getSearchResult(new MyQuerry(new Vector<Filter>(), new CustomerRelation()), new AsyncCallback<ArrayList<SuperModel>>() {
			
			@Override
			public void onSuccess(ArrayList<SuperModel> result) {
				
				for(SuperModel supermodel:result)
				{
					CustomerRelation cust=(CustomerRelation) supermodel;
					orcl.add(cust.getFullName());
				}
					 
			}
			
			@Override
			public void onFailure(Throwable caught) {
				
			}
		});
		 
		 
	}
	public void createScreen()
	{
		initWidget();
		FormFieldBuilder builder;
		builder = new FormFieldBuilder("Branch",olbBranch);
		FormField folbBranch= builder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		builder = new FormFieldBuilder("Sales Person",olbSalesPerson);
		FormField folbSalesPerson= builder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		builder = new FormFieldBuilder("Status",lbStatus);
		FormField flbStatus= builder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		builder = new FormFieldBuilder("From Date",dateComparator.getFromDate());
		FormField fdateComparator= builder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		builder = new FormFieldBuilder("To Date",dateComparator.getToDate());
		FormField fdateComparator1= builder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		
		FormField folbConfigCustomerType= builder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		builder = new FormFieldBuilder("Client name",sbclientname);
		FormField fsbclientname= builder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		builder = new FormFieldBuilder("",personInfo);
		FormField fpersonInfo= builder.setMandatory(false).setRowSpan(1).setColSpan(5).build();
		this.fields=new FormField[][]{
				{fdateComparator,fdateComparator1},
				{flbStatus,folbSalesPerson,folbBranch},
				{fpersonInfo},
				//{fsbclientname}
		};
	}
	public MyQuerry getQuerry()
	{
		Vector<Filter> filtervec=new Vector<Filter>();
		Filter temp=null;
		if(dateComparator.getValue()!=null)
		{
			filtervec.addAll(dateComparator.getValue());
		}
		if(lbStatus.getSelectedIndex()!=0){
			temp=new Filter();temp.setStringValue(lbStatus.getItemText(lbStatus.getSelectedIndex()).trim());
			temp.setQuerryString("businessProcess.status");
			filtervec.add(temp);
		}
		
		if(olbSalesPerson.getSelectedIndex()!=0){
			temp=new Filter();temp.setStringValue(olbSalesPerson.getValue().trim());
			temp.setQuerryString("businessProcess.employee");
			filtervec.add(temp);
		}
		if(olbBranch.getSelectedIndex()!=0){
			temp=new Filter();temp.setStringValue(olbBranch.getValue().trim());
			temp.setQuerryString("businessProcess.branch");
			filtervec.add(temp);
		}
		

		{


			if(personInfo.getIdValue()!=-1)
			{  
				temp=new Filter();
				temp.setIntValue(personInfo.getIdValue());
				temp.setQuerryString("count");
				filtervec.add(temp);
			}

			if(!(personInfo.getFullNameValue().equals("")))
			{
				temp=new Filter();
				temp.setStringValue(personInfo.getFullNameValue());
				temp.setQuerryString("companyName");
				filtervec.add(temp);
			}
			
			
			 if(personInfo.getCellValue()!=-1l)
			  {
			  temp=new Filter();
			  temp.setLongValue(personInfo.getCellValue());
			  temp.setQuerryString("contacts.cellNo1");
			  filtervec.add(temp);
			  }
			/*if(sbclientname.getValueBox().getText().trim()!=null)
			{
				temp=new Filter();
				temp.setStringValue(sbclientname.getValueBox().getText().trim());
				temp.setQuerryString("companyName");
				filtervec.add(temp);
			}*/
		}
		MyQuerry querry= new MyQuerry();
		querry.setFilters(filtervec);
		querry.setQuerryObject(new Customer());
		return querry;
	}
}
